package com.liveinstars.im.endpoint;

import com.google.common.collect.Lists;
import com.liveinstars.api.im.BadWordRecognizeService;
import com.liveinstars.api.im.ChatGroupApi;
import com.liveinstars.api.im.query.ChatGroupQuery;
import com.liveinstars.api.im.vo.ChatGroupDetailVO;
import com.liveinstars.api.im.vo.ChatGroupRecordVO;
import com.liveinstars.api.im.vo.ChatMsgVO;
import com.liveinstars.api.im.vo.WordVO;
import com.liveinstars.im.common.exception.IMRuntimeException;
import com.liveinstars.im.core.service.ChatGroupService;
import com.liveinstars.im.domain.dao.model.ChatGroupMember;
import com.liveinstars.im.domain.manager.ChatGroupMemberManager;
import com.onepiece.shipelves.common.Result;
import com.onepiece.shipelves.common.enums.SystemExceptionEnum;
import com.onepiece.shipelves.common.exception.SystemException;
import com.onepiece.shipelves.common.vo.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author: ting
 * @date: 2020/3/6
 */
@RestController
public class ChatGroupEndpoint implements ChatGroupApi {

    @Autowired
    private ChatGroupService chatGroupService;

    @Autowired
    private ChatGroupMemberManager chatGroupMemberManager;

//    @Autowired
//    @Qualifier("badWordRecognizeApiServiceApiImpl")
//    BadWordRecognizeService badWordRecognizeService;

    @Override
    public Result<Page<ChatGroupRecordVO>> pageBackendRecordByQuery(@RequestBody ChatGroupQuery query) {
        Result<Page<ChatGroupRecordVO>> result = new Result<>();
        try {
            if (query.getPageSize() > 50) {
                query.setPageSize(50);
            }
            Page<ChatGroupRecordVO> page = chatGroupService.pageBackendRecordByQuery(query);
            result.setData(page);
        } catch (Exception e) {
            result = SystemExceptionEnum.UN_KNOW_ERROR.createResult();
        }
        return result;
    }

    @Override
    public Result<Page<ChatGroupRecordVO>> pageByQuery(@RequestBody ChatGroupQuery query) {
        Result<Page<ChatGroupRecordVO>> result = new Result<>();
        try {
            if (query.getPageSize() > 50) {
                query.setPageSize(50);
            }
            Page<ChatGroupRecordVO> page = chatGroupService.pageByQuery(query);
            result.setData(page);
        } catch (Exception e) {
            result = SystemExceptionEnum.UN_KNOW_ERROR.createResult();
        }
        return result;
    }

    @Override
    public Result<ChatGroupDetailVO> getDetailById(@RequestParam("id") Long id) {
        Result<ChatGroupDetailVO> result = new Result<>();
        try {
            ChatGroupDetailVO detail = chatGroupService.getDetailById(id);
            List<Long> chatGroupIdList = Lists.newArrayList();
            chatGroupIdList.add(detail.getId());
            Map<Long, Integer> map = chatGroupMemberManager.mapMemberCountByChatGroupIdList(chatGroupIdList);
            if (map.containsKey(detail.getId())) {
                detail.setUserCount(map.get(detail.getId()));
            }
            result.setData(detail);
        } catch (Exception e) {
            result = SystemExceptionEnum.UN_KNOW_ERROR.createResult();
        }
        return result;
    }

    @Override
    public Result updateById(@RequestBody ChatGroupDetailVO vo) {
        Result result = new Result<>();
        try {
            chatGroupService.updateById(vo);
        } catch (Exception e) {
            result = SystemExceptionEnum.UN_KNOW_ERROR.createResult();
        }
        return result;
    }

    @Override
    public Result updateHotById(@RequestParam("id") Long id, @RequestParam("hot")Integer hot) {
        Result result = new Result<>();
        try {
            chatGroupService.updateHotById(id, hot);
        } catch (Exception e) {
            result = SystemExceptionEnum.UN_KNOW_ERROR.createResult();
        }
        return result;
    }

    @Override
    public Result<Page<ChatMsgVO>> pageChatMsgByChatGroupId(@RequestParam("id")Long id,
                                                            @RequestParam(value = "page_size", defaultValue = "15") Integer pageSize,
                                                            @RequestParam(value = "page_num", defaultValue = "1") Integer pageNum) {
        Result<Page<ChatMsgVO>> result = new Result<>();
        try {
            Page<ChatMsgVO> page = chatGroupService.pageChatMsgByChatGroupId(id, pageSize, pageNum);
            result.setData(page);
        } catch (Exception e) {
            result = SystemExceptionEnum.UN_KNOW_ERROR.createResult();
        }
        return result;
    }

    @Override
    public Result deleteByChatGroupIdAndMsgId(@RequestParam("chat_group_id") Long chatGroupId, @RequestParam("msg_id") Long msgId) {
        Result result = new Result();
        try {
            chatGroupService.deleteByChatGroupIdAndMsgId(chatGroupId, msgId);
        } catch (Exception e) {
            result = SystemExceptionEnum.UN_KNOW_ERROR.createResult();
        }
        return result;
    }

    @Override
    public Result disbandChatGroupByBackend(@RequestParam("chat_group_id") Long chatGroupId) {
        Result result = new Result();
        try {
            chatGroupService.disbandChatGroupByBackend(chatGroupId);
        } catch (Exception e) {
            result = SystemExceptionEnum.UN_KNOW_ERROR.createResult();
        }
        return result;
    }

    @Override
    public Result<List<ChatGroupRecordVO>> listChatGroupByCreateUserId(@RequestParam("user_id") Long userId) {
        Result<List<ChatGroupRecordVO>> result = new Result<>();
        try {
            List<ChatGroupRecordVO> list = chatGroupService.listChatGroupByCreateUserId(userId);
            result.setData(list);
        } catch (Exception e) {
            result = SystemExceptionEnum.UN_KNOW_ERROR.createResult();
        }
        return result;
    }

    @Override
    public Result updateMemberRemarkName(@RequestHeader("uid")Long selfId,
                                         @RequestParam("chat_group_id")Long chatGroupId,
                                         @RequestParam("remark_name")String remarkName) {
        Result result = new Result();
        try {
            chatGroupService.updateMemberRemarkName(selfId, chatGroupId, remarkName);
        } catch (Exception e) {
            if (e instanceof IMRuntimeException) {
                result = ((IMRuntimeException) e).createResult();
            } else if (e instanceof SystemException){
                result.setCode(((SystemException) e).getCode());
                result.setMessage(e.getMessage());
            } else {
                result = SystemExceptionEnum.UN_KNOW_ERROR.createResult();
            }
        }
        return result;
    }

    @Override
    public Result<List<ChatGroupRecordVO>> listChatGroupByCreateUserIdAndInterestId(@RequestParam("user_id") Long userId, @RequestParam("interest_id") Integer interestId) {
        Result<List<ChatGroupRecordVO>> result = new Result<>();
        try {
            List<ChatGroupRecordVO> list = chatGroupService.listChatGroupByCreateUserIdAndInterestId(userId, interestId);
            result.setData(list);
        } catch (Exception e) {
            result = SystemExceptionEnum.UN_KNOW_ERROR.createResult();
        }
        return result;
    }

    @Override
    public ChatGroupRecordVO getChatGroupByCreateUserIdAndInterestId(@RequestParam("user_id") Long userId, @RequestParam("interest_id") Integer interestId) {
        return chatGroupService.getChatGroupByCreateUserIdAndInterestId(userId, interestId);
    }

    @Override
    public boolean addChatGroupMember(@RequestParam("user_id") Long userId, @RequestParam("group_id") Long groupId) {
        ChatGroupMember chatGroupMember = new ChatGroupMember();
        chatGroupMember.setChatGroupId(groupId);
        chatGroupMember.setUserId(userId);
        chatGroupMemberManager.saveChatGroupMember(chatGroupMember);
        return true;
    }

//    @Override
//    public int scoreContent(WordVO wordVO) {
//        badWordRecognizeService.getWordBadLevel()
//        return 0;
//    }


}
